/**
 * Created by xu_jiaHui on 2017/4/9.
 */
$(function () {
    gMainDT = $("#mainDataTables").DataTable({
        ajax: {
            url: "../rds_actor/getActor4Dt",
            type: "POST",
            data: function (d) {
                d.search = "";
            }
        },
        columns: [{
            data: null
        }, {
            data: "actorName"
        }, {
            data: "actorRemarks",
            render: function (data, type, row) {
                if (data == null || data == "")
                    return "暂无描述";
                else return data;
            }
        }, {
            data: "actorId",
            render: function (data, type, row) {
                var buttonDelete = "<button class='btn btn-primary btn-xs btn-flat' onclick='$.deleteActor(" + data + ")' title='删除'><i class='fa fa-minus-square'></i></button>";
                var buttonUpdate = "<button class='btn btn-primary btn-xs btn-flat' onclick='$.updateActor(" + JSON.stringify(row) + ")' title='编辑'><i class='fa fa-edit'></i></button>";
                var buttonOperation = "";
                gUpdate == 0 ? buttonOperation : buttonOperation += buttonUpdate + " ";
                //  gDelete == 0 ? buttonOperation : buttonOperation += buttonDelete;
                buttonOperation += " <button class='btn btn-primary btn-xs btn-flat' onclick='$.getAuthority(" + JSON.stringify(row) + ")' title='权限'><i class='fa fa-key'></i></button>";
                buttonOperation == "" ? buttonOperation = "无权限" : buttonOperation;
                return buttonOperation;
            }
        }],
        dom: "<'row'<'col-md-4'><'#mainToolbar.col-md-8'>r>t<'row'<'col-md-6'><'col-md-6'>>",
        initComplete: function () {
            // 下拉工具栏按钮的权限
            $("#mainToolbar").empty();
            var strToolBar = "<div class='pull-right'><div class='btn-group'><button type='button' class='btn btn-primary btn-sm btn-flat'><i class='fa fa-cog'></i> 操作</button>";
            strToolBar += "<button type='button' class='btn btn-primary btn-sm btn-flat dropdown-toggle' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>";
            strToolBar += "<span class='caret'></span><span class='sr-only'>切换下拉</span></button><ul class='dropdown-menu'>"
            strToolBar += "<li><button href='#' class='btn btn-primary btn-sm btn-flat btn-block' onclick='gMainDT.ajax.reload(null, false);'><i class='fa fa-refresh'></i> 刷新</button></li>";
            var buttonInsert = "<li><button href='#' class='btn btn-primary btn-sm btn-flat btn-block' onclick='$.addActor()'><i class='fa fa-plus-square'></i> 添加</button></li>";
            gInsert == 0 ? strToolBar : strToolBar += buttonInsert;
            strToolBar += "</ul></div> <div class='input-group input-group-sm'><input type='text' class='form-control' name='searchText' placeholder='角色'>";
            strToolBar += "<span class='input-group-btn'><button class='btn btn-primary btn-flat' type='button' onclick='$(this).searchDT()'><i class='fa fa-search'></i></button></span></div></div>";
            $("#mainToolbar").append(strToolBar);
        },
        processing: true,
        serverSide: true,
        ordering: false,
        select: false,
        autoFill: true,
        displayLength: 20,
        lengthMenu: [[5, 10, 15, 20, 50, 100, 150, -1], [5, 10, 15, 20, 50, 100, 150, "All"]],
        language: {
            url: "zh_cn.json"
        },
        fnDrawCallback: function () {// 序号自增回调函数
            var api = this.api();
            var startIndex = api.context[0]._iDisplayStart;
            api.column(0).nodes().each(function (cell, i) {
                cell.innerHTML = startIndex + i + 1;
            });
        }
    });
});
$.deleteActor = function (actorId) {
    bootbox.confirm({
        buttons: {
            confirm: {
                label: '<i class="fa fa-check"></i> 确定',
                className: 'btn btn-primary btn-flat'
            },
            cancel: {
                label: '<i class="fa fa-close"></i> 取消',
                className: 'btn btn-warning btn-flat pull-left'
            }
        },
        message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">确定删除该条数据吗?</b>',
        callback: function (result) {
            if (result) {
                $.ajax({
                    url: "../rds_actor/deleteActorById",
                    type: 'post',
                    dataType: 'json',
                    data: {
                        "actorId": actorId
                    },
                    success: function (msg) {
                        if (msg != null && typeof (msg) != "undefined") {
                            if (msg.isSuccess == true) {
                                if (msg.jsonData > 0) {
                                    $.alert("数据删除成功", 1);
                                    gMainDT.ajax.reload(null, false);
                                } else {
                                    $.alert("数据删除失败", 2);
                                }
                            } else {
                                $.alert("数据删除失败", 2);
                            }
                        } else {
                            $.alert('系统错误，请联系管理员', 2);
                        }
                    },
                    error: function (msg) {
                        $.alert('系统错误，请联系管理员', 2);
                    }
                });
            }
        }
    });
};
$.addActor = function (jsonData) {
    isEdit = false;
    $("#label-actor").text("添加信息");
    $("#modal-actor").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    });
};
// 编辑角色信息
$.updateActor = function (jsonData) {
    isEdit = true;
    $("#actorName").val(jsonData.actorName);
    $("#actorRemarks").val(jsonData.actorRemarks);
    $("#actorId").val(jsonData.actorId);
    $("#label-actor").text("编辑信息");
    $("#modal-actor").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    });
};

$('#form-actor').bootstrapValidator({
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        actorName: {
            validators: {
                stringLength: {min: 1, max: 50, message: '长度限制50'},
                notEmpty: {message: '角色不能为空'}
            }
        },
        actorRemarks: {
            validators: {
                stringLength: {min: 0, max: 255, message: '长度限制255'}
            }
        }
    }
}).on('success.form.bv', function (e) {
    e.preventDefault();
    var $form = $(e.target);
    var formAction = "../rds_actor/addActor";
    if (isEdit == true)
        formAction = "../rds_actor/updateActorById";
    $.ajax({
        url: formAction,
        type: 'post',
        data: $form.serialize(),
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    if (msg.jsonData > 0) {
                        $.alert("数据操作成功", 1);
                        $.clearModal($('#form-actor'), $('#modal-actor'));
                        gMainDT.ajax.reload(null, false);
                    } else {
                        $.alert("数据操作失败", 2);
                    }
                } else {
                    $.alert("数据操作失败", 2);
                    $.clearModal($('#form-actor'), $('#modal-actor'));
                }
            } else {
                $.alert('系统错误，请联系管理员', 2);
                $.clearModal($('#form-actor'), $('#modal-actor'));
            }
        },
        error: function (msg) {
            $.alert('系统错误，请联系管理员', 2);
            $.clearModal($('#form-actor'), $('#modal-actor'));
        }
    });
});
$.getAuthority = function (jsonData) {
    sessionStorage.setItem("actorId", jsonData.actorId);
    //记录这个页面的权限值
    sessionStorage.setItem("authority", gInsert + "," + gDelete + "," + gUpdate + "," + gSelect + "," + gCheck);
    $.loadURL('角色信息', jsonData.actorName, "system/menu.html", 1, 1, 0, 1, 0);
};